# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from app.modules.common.dbUtil import createId


class EntranceInfo(db.Model, JsonEncoder):
    __tablename__ = "entrance"
    id = db.Column(db.String, primary_key = True)
    type = db.Column(db.String)
    hosts = db.Column(db.String)
    user = db.Column(db.String)
    current_status = db.Column(db.String)
    expect_status = db.Column(db.String)
    env_name = db.Column(db.String)
    zone_code = db.Column(db.String)
    prefix = db.Column(db.String)
    suffix = db.Column(db.String)
    update_time = db.Column(db.DATETIME)
    error_desc = db.Column(db.String)
    group = db.Column(db.String)
    is_overtime = db.Column(db.String)
    apps = None

    def __init__(self, type = None, hosts = None, user = None, current_status = None, expect_status = None,
                 zone_code = None, prefix = None, suffix = None, update_time = None, error_desc = None, group = None,
                 env_name = None,is_overtime='1'):
        self.id = createId('en')
        self.type = type
        self.hosts = hosts
        self.user = user
        self.current_status = current_status
        self.expect_status = expect_status
        self.zone_code = zone_code
        self.prefix = prefix
        self.suffix = suffix
        self.update_time = update_time
        self.error_desc = error_desc
        self.group = group
        self.env_name = env_name
        self.is_overtime = is_overtime

    def to_dict(self):
        return {
            'id': self.id,
            'type': self.type,
            'hosts': self.hosts,
            'user': self.user,
            'current_status': self.current_status,
            'expect_status': self.expect_status,
            'zone_code': self.zone_code,
            'prefix': self.prefix,
            'suffix': self.suffix,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
            'error_desc': self.error_desc,
            'group': self.group,
            'env_name': self.env_name,
            'is_overtime': self.is_overtime,
        }
